import pygame
from pygame.locals import *
import sys
import os
from tankSnake import tank_snake_runner
from marieSnake import marie_snake_runner

SCREEN_X, SCREEN_Y = 1200, 750
os.environ['SDL_VIDEO_CENTERED'] = '1'

def main():
    while True:
        pygame.init()
        title_font = pygame.font.SysFont('simsun', 48)
        welcome_words = title_font.render('Welcome to FC Snake', True, (237, 109, 0), (0, 0, 0))
        tips_font = pygame.font.SysFont('arial', 32, bold=True)
        tank_game_words = tips_font.render('Battle city mode', True, (237, 109, 0), (0, 0, 0))
        marie_game_words = tips_font.render('Super marie mode', True, (237, 109, 0), (0, 0, 0))
        align_left = SCREEN_X // 8
        align_right = 4 * SCREEN_X // 8
        align_top = SCREEN_Y // 4
        text_height = SCREEN_Y // 15

        playSurface = pygame.display.set_mode((SCREEN_X, SCREEN_Y))  # 全屏显示
        fpsClock = pygame.time.Clock()
        pygame.display.set_caption(u'贪吃蛇')  # 欢迎界面，此时未开始游戏

        game_mode = None
        waiting_select = True
        while waiting_select:
            for event in pygame.event.get():
                if event.type == QUIT:
                    sys.exit()
                elif event.type == KEYDOWN:
                    if event.key == K_ESCAPE:  #按动ESC退出游戏
                        pygame.event.post(pygame.event.Event(QUIT))
                elif waiting_select and event.type == pygame.MOUSEBUTTONDOWN: #在游戏欢迎界面时，根据鼠标位置判断是否开始游戏
                    x, y = pygame.mouse.get_pos()
                    if align_left <= x <= align_right and align_top + 2*text_height <= y <= align_top + 3*text_height:
                        waiting_select = False
                        game_mode = 'tank'
                    elif align_left <= x <= align_right and align_top + 3*text_height <= y <= align_top + 4*text_height:
                        waiting_select = False
                        game_mode = 'marie'

            playSurface.blit(welcome_words, (align_left, align_top))
            playSurface.blit(tank_game_words, (align_left, align_top + 2*text_height))
            playSurface.blit(marie_game_words, (align_left, align_top + 3*text_height))
            pygame.display.update()
            fpsClock.tick(5)

        if game_mode is not None:
            if game_mode == 'tank':
                tank_snake_runner(SCREEN_X, SCREEN_Y)
            elif game_mode == 'marie':
                marie_snake_runner(SCREEN_X, SCREEN_Y)


if __name__ == '__main__':
    main()